<template>
    <div class="carBox">
   <el-table
    ref="multipleTableRef"
    :data="$store.getters['cart/validList']"
    style="width: 100%"
    @selection-change="handleSelectionChange"
    @select='select'
  >
    <el-table-column type="selection" width="55"/>
    <el-table-column  label="商品信息"   >
   <template #default="scope" >
  
<div class="goodMsg">
   <img :src="scope.row.picture" alt="">
   <div class="msg">
    <span>{{scope.row.name}}</span>
    <p>{{scope.row.attrsText}}<van-icon name="arrow-down" /></p>
   </div>
</div>
   </template>
    </el-table-column>
    <el-table-column property="price" label="单价" />
    <el-table-column property="count" label="数量" > 
    <template #default="scope">
        <!-- <span>{{scope.row.price*scope.row.count}}</span> -->
        <van-stepper v-model="scope.row.count" disable-input @change='change(scope.row.skuId,scope.row.count)' />
     </template>
    </el-table-column>
     <el-table-column  label="小计"  >
     <template #default="scope">
        <span>{{scope.row.price*scope.row.count}}</span>
     </template>
     </el-table-column>
      <el-table-column label="操作">
      <template #default="scope">
         <p class="p"><a @click="batchDeleteCart()" href="javascript:;">移入收藏夹</a></p>
         <p class="del" @click="del(scope.row)"><a href="javascript:;">删除</a></p>
         <p class="p"><a @click="batchDeleteCart(true)" href="javascript:;">找相似</a></p>
      </template>
      </el-table-column>
        <template #empty>
         <el-empty description="购物车里还没有宝贝！" />
      </template>

  </el-table>
  <div class="total">
  <div class="contain">
 <div class="left">
    <el-checkbox v-model="$store.getters['cart/validList']" label="全选" size="large" @change='sel' />
 </div>
<div class="right">
共{{$store.getters['cart/validTotal']}}件商品，选中{{$store.getters['cart/selectedTotal']}}件商品，商品合计 {{$store.getters['cart/selectedAmount']}}
<el-button>下单结算</el-button>
</div>
  </div> 
  </div>

    </div>
</template>

<script setup>
import { useStore, } from 'vuex'
import {ref,onMounted,nextTick} from 'vue'
import { ElMessage,ElMessageBox } from 'element-plus'
const store = useStore()
const change = (skuId,count) =>{
console.log(skuId,count)
store.dispatch('cart/updateCart', { skuId, count })
}

const multipleTableRef= ref()
 
//  调用回显函数
nextTick(() => {
        selectShow()
   })
// 单个选项
const select= (selection, row)=>{
console.log('单个选项变化',row)
console.log('多个selection',selection)
const newSel = !row.selected
console.log('1row.selected',row.name,row.selected,newSel)
store.dispatch('cart/updateCart',{skuId:row.skuId,selected:newSel})
console.log('2row.selected',row.name,row.selected,newSel)
selectShow()
}
//  回显函数
  const selectShow = () =>{
    store.getters['cart/validList'].forEach((row) => {  //表格的数据
        // console.log('row回显',row)
        multipleTableRef.value.toggleRowSelection(row, row.selected);  //根据selected来判断是否选中
        // console.log('显示回显')
     })
  }
  const  handleSelectionChange = (row) =>{
        console.log('选择',row)
        


}
const sel = (val) =>{
    console.log(val)
    
    // store.getters['cart/selectedTotal'] = 
}
const  selectAll =()=>{
    console.log('全选')
}

// const  handleSelectionChange = (rows) => {
//       this.selectionKeys = rows.map(item => item.id);
//       console.log(this.selectionKeys)
//     }
// const selectionRow =(val) =>{
// console.log('val',val)
// }
const del = (row) =>{
     ElMessageBox.confirm(
    '是否移除购物车',
    {
      confirmButtonText: 'OK',
      cancelButtonText: 'Cancel',
      type: 'warning',
    }
  )
    .then(() => {
    console.log(row,row.id)
    store.dispatch('cart/deleteCart',row.id)
      ElMessage({
        type: 'success',
        message: '商品已移除',
      })
    })
    .catch(() => {
      ElMessage({
        type: 'info',
        message: '已取消',
      })
    })
  
//     ElMessage({
//     message: '删除成功',
//     type: 'success',
//   })
      
}

onMounted(()=>{


    })

</script>

<style lang="less" scoped>
.carBox{
    width: 100%;
    min-height: 200px;
    background-color: #fff;
}
.table__header-wrapper{
    color: #666;
    font-size: 16px;
}
.el-table{

    img{
        width: 100px;
        height: 100px;
    }
}
.goodMsg{
    display: flex;
    align-items: center;
    span{
        font-size: 16px;
        color: #666;
    }
    p{
        color: #999;
        margin-top: 10px;
    }
    .msg{
        margin-left: 5px;}
}
.p{
    a{
        color: #333;
    }
}
.del{
    a{
        color: #27BA9B;
    }
}
.carBox .contain{
    margin-top: 20px;
    padding: 20px;
    display: flex;
    justify-content: space-between;
}
.right{
    .el-button{
        width: 150px;
        padding: 10px;
        height: 50px;
        margin-left: 20px;
        background-color: #27BA9B;
        color: #fff;
    }
}

</style>